﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BasicComponents.pub
{
    /// <summary>
    /// Interaction logic for UC_LabelTextbox_Btn.xaml
    /// </summary>
    public partial class UC_LabelTextbox_Btn : UserControl
    {
        //该参数保存输入框监测到的输入事件
        KeyEventArgs keyEventArgs = null;
        public KeyEventArgs KeyEventArgs { get { return keyEventArgs; } set { keyEventArgs = value; } }
        public UC_LabelTextbox_Btn()
        {
            InitializeComponent();
        }

        static void resetComponentSize(UC_LabelTextbox_Btn sender)
        {
            // sender.Width = sender.column_title.Width.Value + sender.column_value.Width.Value + sender.columns_btn.Width.Value;
        }
        //1、标题
        public String TitileVal
        {
            get { return (String)GetValue(TitileValProperty); }
            set { SetValue(TitileValProperty, value); }
        }
        public static readonly DependencyProperty TitileValProperty =
            DependencyProperty.Register("TitileVal", typeof(String), typeof(UC_LabelTextbox_Btn), new PropertyMetadata("标题", (s, e) =>
            {
                UC_LabelTextbox_Btn sender = s as UC_LabelTextbox_Btn;
                sender.label_titile.Content = e.NewValue.ToString();
                // resetComponentSize(sender);
            }));

        //2、内容块内容信息
        public String TxtValue
        {
            get { return (String)GetValue(TxtValueProperty); }
            set { SetValue(TxtValueProperty, value); }
        }
        public static readonly DependencyProperty TxtValueProperty = DependencyProperty.Register("TxtValue", typeof(String), typeof(UC_LabelTextbox_Btn), new PropertyMetadata("", (s, e) =>
        {
            UC_LabelTextbox_Btn sender = s as UC_LabelTextbox_Btn;
            sender.txt_value.Text = e.NewValue.ToString();
            //resetComponentSize(sender);
        }));

        //3、内容框长度 
        public Double TxtWidth
        {
            get { return (Double)GetValue(TxtWidthProperty); }
            set { SetValue(TxtWidthProperty, value); }
        }


        public static readonly DependencyProperty TxtWidthProperty = DependencyProperty.Register("TxtWidth", typeof(Double), typeof(UC_LabelTextbox_Btn), new PropertyMetadata(120.00, (s, e) =>
        {
            UC_LabelTextbox_Btn sender = s as UC_LabelTextbox_Btn;
            sender.txt_value.Width = Double.Parse(e.NewValue.ToString()); 
        }));
        //3、标题框长度 
        public Double TitleWidth
        {
            get { return (Double)GetValue(TitleWidthProperty); }
            set { SetValue(TitleWidthProperty, value); }
        }
        
        public static readonly DependencyProperty TitleWidthProperty = DependencyProperty.Register("TitleWidth", typeof(Double), typeof(UC_LabelTextbox_Btn), new PropertyMetadata(60.00, (s, e) =>
        {
            UC_LabelTextbox_Btn sender = s as UC_LabelTextbox_Btn;
            sender.label_titile.Width = Double.Parse(e.NewValue.ToString()); 
        }));
        //、整体框长度 
        public Double BtnWidth
        {
            get { return (Double)GetValue(WidthProperty); }
            set { SetValue(WidthProperty, value); }
        }
        public static readonly DependencyProperty BtnWidthProperty = DependencyProperty.Register("Width", typeof(Double), typeof(UC_LabelTextbox_Btn), new PropertyMetadata(120.00, (s, e) =>
         {
             UC_LabelTextbox_Btn sender = s as UC_LabelTextbox_Btn;  
             sender.txt_btn.Width = Double.Parse(e.NewValue.ToString());
         }));

        //、整体框高度
        public new Double Height
        {
            get { return (Double)GetValue(HeightProperty); }
            set { SetValue(HeightProperty, value); }
        }


        public static readonly new DependencyProperty HeightProperty = DependencyProperty.Register("Height", typeof(Double), typeof(UC_LabelTextbox_Btn), new PropertyMetadata(120.00, (s, e) =>
         {
             UC_LabelTextbox_Btn sender = s as UC_LabelTextbox_Btn;
             sender.row_maginBox.Height= new GridLength(Double.Parse(e.NewValue.ToString()));
             sender.row_txtBox.Height= new GridLength(Double.Parse(e.NewValue.ToString()));
             sender.txt_value.Height = Double.Parse(e.NewValue.ToString()) - 2; 
             sender.label_titile.Height = sender.txt_value.Height;
         }));

        //4、按钮点击事件
        public event RoutedEventHandler BtnClick
        {
            add { this.AddHandler(BtnClickEvent, value); }
            remove { this.RemoveHandler(BtnClickEvent, value); }
        }
        public static readonly RoutedEvent BtnClickEvent = EventManager.RegisterRoutedEvent("BtnClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UC_LabelTextbox_Btn));

        private void txt_btn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //MessageBox.Show(this.ToString());
                RoutedEventArgs args = new RoutedEventArgs(UC_LabelTextbox_Btn.BtnClickEvent, this);
                RaiseEvent(args);
            }
            catch (Exception e1)
            {
                File.AppendAllText("log_wxt.txt", "----" + DateTime.Now.ToString("yyyyMMdd:hh mm ss") + "----\r\n" + "\r\n" + e1.Message);
            }
        }

        //5、内容输入事件
        public event RoutedEventHandler TxtKeyDown
        {
            add { this.AddHandler(TxtKeyDownEvent, value); }
            remove { this.RemoveHandler(TxtKeyDownEvent, value); }
        }
        public static readonly RoutedEvent TxtKeyDownEvent = EventManager.RegisterRoutedEvent("TxtKeyDown", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UC_LabelTextbox_Btn));

        //输入框内容变化事件
        private void txt_value_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.TxtValue = this.txt_value.Text;
        }
        //按键按下事件
        private void txt_value_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                this.KeyEventArgs = e;
                RoutedEventArgs args = new RoutedEventArgs(UC_LabelTextbox_Btn.TxtKeyDownEvent, this);
                RaiseEvent(args);
            }
            catch (Exception e1)
            {
                MessageBox.Show("" + e1.Message);
            }
        }

        ////isenable属性 
        //public Boolean IsEnableTxtBtn
        //{
        //    get { return (Boolean)GetValue(TitileValProperty); }
        //    set { SetValue(TitileValProperty, value); }
        //}
        //public static readonly DependencyProperty IsEnableTxtBtnProperty =
        //    DependencyProperty.Register("IsEnableTxtBtn", typeof(Boolean), typeof(UC_LabelTextbox_Btn), new PropertyMetadata(false, (s, e) =>
        //    {
        //        UC_LabelTextbox_Btn sender = s as UC_LabelTextbox_Btn;
        //        sender.txt_value.IsEnabled = (Boolean)e.NewValue;
        //        sender.txt_btn.IsEnabled = (Boolean)e.NewValue;
        //        // resetComponentSize(sender);
        //    }));

    }
}
